using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using Workflows.Components;
using Workflows.Diagrams.Properties;

namespace Workflows.Diagrams
{
    public delegate void ToolItemSelected(WorkflowComponentBase wfComponent);

    public class ToolItemBase : System.Windows.Forms.Button, IComparable
    {
        public event ToolItemSelected ToolItemSelectedEvent;
        private string _description;
        private string _category;
        private WorkflowComponentBase _wfComponent;

        public ToolItemBase()
            : base()
        {
            System.Windows.Forms.ImageList imgList = new System.Windows.Forms.ImageList();
            imgList.Images.Add(Resources.ipedit);
            this.ImageList = imgList;
            this.ImageIndex = 0;
            this.Text = "  basic task";
            this.ImageAlign = ContentAlignment.MiddleLeft;
            this.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
            this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.FlatAppearance.BorderColor = System.Drawing.Color.GhostWhite;
            this.FlatAppearance.BorderSize = 0;
            this.Dock = System.Windows.Forms.DockStyle.Top;
            this.BackColor = System.Drawing.SystemColors.Control;
            this.AutoSize = false;
            this.Size = new System.Drawing.Size(200, 25);
            this.Category = WorkflowComponentCategory.Misc.ToString();
            this.Description = "basic workflow task";
            this.WorkflowComponent = null;
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(ToolItemBase_MouseDown);
            this.MouseHover += new EventHandler(ToolItemBase_MouseHover);
            this.MouseLeave += new EventHandler(ToolItemBase_MouseLeave);
        }

        void ToolItemBase_MouseLeave(object sender, EventArgs e)
        {
            this.BackColor = System.Drawing.SystemColors.Control;
        }

        void ToolItemBase_MouseHover(object sender, EventArgs e)
        {
            this.BackColor = System.Drawing.Color.GhostWhite;
        }

        void ToolItemBase_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (this.ToolItemSelectedEvent != null)
            {
                this.ToolItemSelectedEvent(this.WorkflowComponent);
            }
        }

        #region properties
        public string Description
        {
            get
            {
                return this._description;
            }
            set
            {
                this._description = value;
            }
        }
        public string Category
        {
            get
            {
                return this._category;
            }
            set
            {
                this._category = value;
            }
        }
        public WorkflowComponentBase WorkflowComponent
        {
            get
            {
                return this._wfComponent;
            }
            set
            {
                this._wfComponent = value;
            }
        }

        #endregion

        #region Implementation of IComparable

        /// <summary>
        /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
        /// </summary>
        /// <returns>
        /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings: 
        ///                     Value 
        ///                     Meaning 
        ///                     Less than zero 
        ///                     This instance is less than <paramref name="obj"/>. 
        ///                     Zero 
        ///                     This instance is equal to <paramref name="obj"/>. 
        ///                     Greater than zero 
        ///                     This instance is greater than <paramref name="obj"/>. 
        /// </returns>
        /// <param name="obj">An object to compare with this instance. 
        ///                 </param><exception cref="T:System.ArgumentException"><paramref name="obj"/> is not the same type as this instance. 
        ///                 </exception><filterpriority>2</filterpriority>
        public int CompareTo(object obj)
        {
            if(obj.GetType()==typeof(ToolItemBase))
            {
                ToolItemBase otherToolItem = (ToolItemBase) obj;
                return this.Text.CompareTo(otherToolItem.Text);
            }
            return 0;
        }

        #endregion
    }

    
}
